package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.GroupAnnouncementMapper;
import com.zcoj.pojo.Dto.GroupAnnouncement;
import com.zcoj.service.GroupAnnouncementService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/22 13:41
 */
@Service
public class GroupAnnouncementServiceImpl implements GroupAnnouncementService {


    @Autowired
    private GroupAnnouncementMapper groupAnnouncementMapper;

    @Override
    public Integer create(GroupAnnouncement groupAnnouncement) {
        groupAnnouncementMapper.insert(groupAnnouncement);
        return groupAnnouncement.getId();
    }

    @Override
    public void deleteById(Integer id) {
        groupAnnouncementMapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        groupAnnouncementMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(GroupAnnouncement groupAnnouncement) {
        groupAnnouncementMapper.updateById(groupAnnouncement);
    }

    @Override
    public void updateByIdInBatch(List<GroupAnnouncement> groupAnnouncements) {
        if (CollectionUtils.isEmpty(groupAnnouncements)) return;
        for (GroupAnnouncement groupAnnouncement: groupAnnouncements) {
            this.updateById(groupAnnouncement);
        }
    }

    @Override
    public GroupAnnouncement findById(Integer id) {
        return groupAnnouncementMapper.selectById(id);
    }

    @Override
    public GroupAnnouncement findOne(GroupAnnouncement groupAnnouncement) {
        Wrapper<GroupAnnouncement> wrapper = new QueryWrapper<>(groupAnnouncement);
        return groupAnnouncementMapper.selectOne(wrapper);
    }

    @Override
    public List<GroupAnnouncement> findList(GroupAnnouncement groupAnnouncement) {
        Wrapper<GroupAnnouncement> wrapper = new QueryWrapper<>(groupAnnouncement);
        return groupAnnouncementMapper.selectList(wrapper);
    }

    @Override
    public PageData<GroupAnnouncement> findPage(PageWrap<GroupAnnouncement> pageWrap) {
        IPage<GroupAnnouncement> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<GroupAnnouncement> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(groupAnnouncementMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(GroupAnnouncement groupAnnouncement) {
        Wrapper<GroupAnnouncement> wrapper = new QueryWrapper<>(groupAnnouncement);
        return groupAnnouncementMapper.selectCount(wrapper);
    }
}
